
from django.http import JsonResponse
from backend.models import UserInfo
from django.views.decorators.csrf import csrf_exempt


# 获取用户数据接口
@csrf_exempt
def user_info(request):
    response = {}
    try:
        username = request.GET.get('username')
        phone = request.GET.get('phone_number')
        cid = request.GET.get('id')
        if username and phone:
            info = UserInfo.objects.filter(username=username, phone_number=phone)
        elif phone:
            info = UserInfo.objects.filter(phone_number=phone)
        elif username:
            info = UserInfo.objects.filter(username=username)
        elif cid:
            info = UserInfo.objects.filter(id=cid)
        else:
            info = UserInfo.objects.all()
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
        response['list'] = list(info.values())
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)


# 删除用户接口
@csrf_exempt
def user_del(request):
    response = {}
    try:
        cid = request.GET.get('id')
        if cid:
            UserInfo.objects.filter(id=cid).delete()
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


# 修改用户信息接口
@csrf_exempt
def user_edit(request):
    response = {}
    try:
        cid = request.GET.get('id')
        username = request.GET.get('username')
        psd = request.GET.get('password')
        phone = request.GET.get('phone_number')
        email = request.GET.get('email')
        danceType = request.GET.get('danceType')
        UserInfo.objects.filter(id=cid).update(username=username, password=psd, phone_number=phone, email=email,
                                               danceType=danceType)
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)